// pages/home/home.js
const api = require("../../comm/api")
const http = require("../../utils/http")
const util = require("../../utils/util")
import Notify from '../../components/@vant/weapp/notify/notify';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        rotations: [],
        services: [],
        hots: [],
        categorys: [],
        newses: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    onShow() {
        this.onPullDownRefresh()
    },

    onPullDownRefresh() {
        // 加载轮播图
        this.loadRotations()
        // 加载服务
        this.loadServices()
        // 加载热点新闻
        this.loadHots()
        // 加载新闻类别
        this.loadCategorys()
    },

    loadRotations() {
        http.getJSON(api.AdRotation.Rotation.PATH, pojo => {
            this.setData({
                rotations: pojo.data
            })
        }, err => {
            Notify({
                type: 'warning',
                message: JSON.stringify(err)
            })
        })
    },

    loadServices() {
        http.getJSON(api.Service.List.PATH, pojo => {
            const services = pojo.data.slice(0, 9)
            this.setData({
                services: services
            })
        }, err => {
            Notify({
                type: 'warning',
                message: JSON.stringify(err)
            })
        })
    },

    loadHots() {
        http.getJSON(api.Press.List.PATH + "?hot=Y", pojo => {
            const hots = pojo.data.slice(0, 2)
            this.setData({
                hots: hots
            })
        }, err => {
            Notify({
                type: 'warning',
                message: JSON.stringify(err)
            })
        })
    },

    loadCategorys() {
        http.getJSON(api.Press.Category.List.PATH, pojo => {
            const categorys = pojo.data
            this.setData({
                categorys: categorys
            })
            const cid = categorys[0].id
            this.loadNewses({
                detail: {
                    name: cid
                }
            }, err => {
                Notify({
                    type: 'warning',
                    message: JSON.stringify(err)
                })
            })
        }, err => {
            Notify({
                type: 'warning',
                message: JSON.stringify(err)
            })
        })
    },

    loadNewses(e) {
        const cid = e.detail.name
        const path = api.Press.List.PATH + '?type=' + cid
        http.getJSON(path, pojo => {
            const newses = pojo.data
            this.setData({
                newses: newses
            })
        }, err => {
            Notify({
                type: 'warning',
                message: JSON.stringify(err)
            })
        })
    },

    onNewsSearch(e) {
        const keyword = e.detail
        wx.navigateTo({
            url: '/pages/press/list/list?keyword=' + keyword,
        })
    },

    showNews(e) {
        wx.navigateTo({
            url: '/pages/press/detail/detail?id=' + e.currentTarget.dataset.id,
        })
    },

    onService(e) {
        const serviceName = e.currentTarget.dataset.name
        if (serviceName == '全部服务') {
            wx.switchTab({
                url: '/pages/services/services',
            })
        } else {
            Notify({
                type: 'primary',
                message: serviceName + '正在上架中，请稍候...'
            })
        }
    }
})